local current
local state

local blacklistName = KEYS[1]
local sourceIp = KEYS[2]
local uniqueKey = KEYS[3]

local expire = ARGV[1]
local limit = ARGV[2]

state = redis.call('GET', blacklistName .. sourceIp)
if state then
    return -1
end

local key = blacklistName .. ':' .. sourceIp .. uniqueKey
current = redis.call('INCR', key)
if tonumber(current) == 1 then
    redis.call("EXPIRE", key, expire)
end

if tonumber(current) > tonumber(limit) then
	redis.call("SET", blacklistName .. sourceIp, '1', 'EX', '3600')
end
	
return tonumber(current)
